package com.pro.service.imple;

import com.pro.mapper.JsMenuMapper;
import com.pro.pojo.JsMenu;
import com.pro.pojo.JsUser;
import com.pro.pojo.JsonData;
import com.pro.service.JsMenuService;
import com.pro.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.logging.Logger;

/**
 * ClassName:JsMenuServiceImple
 * Package:com.pro.service.imple
 * Description:
 *
 * @Date:2021/12/30 13:45
 * @Author: ZPT
 */
@Service
public class JsMenuServiceImple implements JsMenuService {

    @Autowired(required = false)
    private JsMenuMapper jsMenuMapper;

    @Autowired
    private RedisCache redisCache;
    @Override
    public List<JsMenu> getMenu(Integer userId,Integer parentId) {
        //获取user
        JsUser user = (JsUser) redisCache.getCacheObject("user");
        //获取redis导航栏数据
        List<JsMenu> jsMenuList = redisCache.getCacheList("menu" + user.getUserId());
        //是否为空
        if (!jsMenuList.isEmpty()){
            return jsMenuList;
        }
        //查询导航栏数据
        List<JsMenu> menuList = jsMenuMapper.getMenu(user.getUserId(), 0);
        for (JsMenu jsMenu : menuList) {
            jsMenu.setChildren(jsMenuMapper.getMenu(user.getUserId(),jsMenu.getMenuId()));
            List<JsMenu> menuChildren = jsMenu.getChildren();
            for (JsMenu menuChild : menuChildren) {
                menuChild.setChildren(jsMenuMapper.getMenu(user.getUserId(), menuChild.getMenuId()));
            }
        }
        redisCache.setCacheList("menu"+user.getUserId(),menuList);
        return menuList;
    }
}
